var addNewWorkspace;
columnNames = ";ID;ClientID;Display Name;First Name;Last Name;Email;Role;Created Date;Created Date;Status;Note".split(";");
columnModel = [{ name: "Edit", index: "Edit", width: 20, height: 20, align: "left", hidedlg: !0, fixed: !0, search: !1, classes: "editrow", title: !0, hidden: !1, widthOrg: 20, resizable: !0, sortable: !0, stype: "text" }, { name: "pkUser_ID", index: "pkUser_ID", width: 1, editable: !1, hidedlg: !0, hidden: !0, title: !0, widthOrg: 1, resizable: !0, sortable: !0, search: !0, stype: "text" }, { name: "fkClient_ID", index: "fkClient_ID", width: 1, editable: !1, hidedlg: !0, hidden: !0, title: !0, widthOrg: 1, resizable: !0, sortable: !0, search: !0, stype: "text" }, {
        name: "DisplayName",
        index: "DisplayName",
        width: 80,
        editable: !1,
        hidden: !1,
        title: !0,
        widthOrg: 80,
        resizable: !0,
        sortable: !0,
        search: !0,
        stype: "text"
    }, { name: "FirstName", index: "FirstName", width: 80, editable: !1, hidden: !1, title: !0, widthOrg: 80, resizable: !0, sortable: !0, search: !0, stype: "text" }, { name: "LastName", index: "LastName", width: 80, editable: !1, hidden: !1, title: !0, widthOrg: 80, resizable: !0, sortable: !0, search: !0, stype: "text" }, {
        name: "Email",
        index: "Email",
        width: 100,
        editable: !1,
        hidden: !1,
        title: !0,
        widthOrg: 100,
        resizable: !0,
        sortable: !0,
        search: !0,
        stype: "text"
    }, { name: "RoleName", index: "UserRoleName", width: 60, align: "left", editable: !1, hidden: !1, title: !0, widthOrg: 60, resizable: !0, sortable: !0, search: !0, stype: "text" }, { name: "CreatedOn", index: "Title", width: 80, align: "center", editable: !1, title: !0, hidden: !1, widthOrg: 80, resizable: !0, sortable: !0, search: !0, stype: "text" }, { name: "LastModifiedBy", index: "Phone1", width: 40, align: "center", editable: !1, hidden: !1, searchoptions: {}, title: !0, widthOrg: 40, resizable: !0, sortable: !0, search: !0, stype: "text" }, {
        name: "Created Date",
        index: "Phone2",
        width: 60,
        align: "center",
        editable: !1,
        hidden: !1,
        searchoptions: {},
        title: !0,
        widthOrg: 60,
        resizable: !0,
        sortable: !1,
        search: !0,
        stype: "text"
    }, { name: "Note", index: "Address1", width: 80, align: "left", editable: !1, hidden: !0, searchoptions: {}, title: !0, widthOrg: 80, resizable: !0, sortable: !0, search: !0, stype: "text" }];
$(document).ready(function() {
    var j, k;
    $("#userTab").attr("class", "navon").children("span").attr("class", "navspanon");
    var d = !1, g, c, l = jQuery("#list"), m = $("#glasswrapper"), f = 0;
    gridconfigtype = "UserGrid";
    $("#resubmitter").dialog({ autoOpen: !1, minWidth: 475, minHeight: 240, resizable: !1, close: function() { m.hide() } });
    GetVCConfig();
    $(".detailcancel").click(function() {
        frmvisualvalid = $("#frmuserdetails").jVal({ style: "cover", padding: 3, border: 1, wrap: !0 });
        $("#frmuserdetails").stop().find(".jfVal,.jValCover").stop().remove().end().find(".jVal,[jVal]").css({
            position: "",
            borderColor: "",
            left: "0px",
            top: "0px"
        }).parent().find(".jValRelWrap").remove();
        $("#frmuserdetails").stop().find(".jValSpacer,.jValSpacercover").stop().remove().end();
        CancelClick();
        return !1
    });
    $(".detaildelete").click(function() {
        DeactivateUser();
        return !1
    });
    $(".detailsave").click(function() {
        SaveUser();
        return !1
    });
    $(".detailadd").click(function() {
        SaveNewUser();
        return !1
    });
    $("#up_ResetPass").click(function() {
        confirm("Resetting the user password sends an email notification to the user with a new temporary password and a login link. The email also provides instructions on how to create a new permanent password. \n\nPlease click OK to confirm.") &&
            ResetPassword($("#display_email").html(), "Forced");
        return !1
    });
    $(".form_table input:checkbox").click(function() { promptSave() });
    $(".form_txt").keydown(function() { promptSave() });
    $(".form_txtarea").keydown(function() { promptSave() });
    $(".form_select").change(function() { promptSave() });
    addNewWorkspace = function() {
        d = !0;
        k("");
        j();
        $(".detailadd").show();
        $(".detailsave").hide();
        $(".detailactivate").hide()
    };
    k = function(a) {
        $("#work_cboEnvironment").html("");
        var b = "", e = FillDropdown("Environment");
        $.each(e, function(c) {
            b +=
                "<option value='" + e[c] + "' ";
            a == e[c] && (b += "selected");
            b += " >" + e[c] + "</option>"
        });
        $("#work_cboEnvironment").html(b)
    };
    j = function() {
        $("#panel_grid_main").hide();
        $("#panel_grid_sub").show();
        $("#panel_tabs").show();
        preSave(d);
        try {
            var a = $("#second_grid_container").width();
            $("#second_list").jqGrid("setGridWidth", a - 2)
        } catch(b) {
        }
    };
    FillUserForm = function(a) {
        ShowDetailNoNav();
        $(".detailadd").hide();
        $(".detailsave").show();
        $(".detaildelete").show();
        $(".notnew").show();
        $(".isnew").hide();
        FillClientRoleDDL(a.ClientRoleName);
        $("#hidID").val(a.pkUser_ID);
        $("#user_email").val("");
        $("#user_email").hide();
        $("#display_email").html(a.Email);
        $("#user_firstname").val(a.FirstName);
        $("#user_lastname").val(a.LastName);
        $("#user_role").val(a.ClientRoleName);
        $("#user_title").val(a.Title);
        $("#user_phone1").val(a.Phone1);
        $("#user_phone2").val(a.Phone2);
        $("#user_address1").val(a.Address1);
        $("#user_address2").val(a.Address2);
        $("#user_city").val(a.City);
        $("#user_zipcode").val(a.PostalCode);
        $("#user_state").val(a.StateProv);
        $("#user_country").val(a.Country);
        a.IsCurrent ? $(".detaildelete").button("option", "disabled", !0) : $(".detaildelete").button("option", "disabled", !1)
    };
    FillGridRowClass = function() {
        var a = !0, b = { ClientRoleName: "", Email: "" };
        b.pkUser_ID = $("#hidID").val();
        b.FirstName = $("#user_firstname").val();
        b.LastName = $("#user_lastname").val();
        b.ClientRoleName = $("#user_role").val();
        b.Title = $("#user_title").val();
        b.Phone1 = $("#user_phone1").val();
        b.Phone2 = $("#user_phone2").val();
        b.Address1 = $("#user_address1").val();
        b.Address2 = $("#user_address2").val();
        b.City =
            $("#user_city").val();
        b.PostalCode = $("#user_zipcode").val();
        b.StateProv = $("#user_state").val();
        b.Country = $("#user_country").val();
        0 < $("#display_email").html().length && (b.Email = $("#display_email").html());
        0 < $("#user_email").val().length && (b.Email = $("#user_email").val());
        1 > b.FirstName.length && (a = !1);
        1 > b.LastName.length && (a = !1);
        1 < b.Email.length ? /^([a-zA-Z0-9.!#$%&*+-/=?\^_`{|}~-]+@[a-zA-Z0-9_-]+(?:\.[a-zA-Z0-9_-]+)*)$/.test(b.Email) || (a = !1) : a = !1;
        1 > b.Phone1.length && (a = !1);
        if (null == b.ClientRoleName || 1 >
            b.ClientRoleName.length) a = !1;
        a || (b.valid = !1);
        return b
    };
    addNewUser = function() {
        var a = $("#hidsearchid").val();
        d = !0;
        $.ajax({
            type: "POST", contentType: "application/json; charset=utf-8", url: "/Service/Services.aspx/AddRecentDetail", data: '{seid:"' + a + '", key:0, intrnl:' + d + "}", dataType: "json",
            success: function() {
            },
            error: function() {
            }
        });
        $("#hidID").val("0");
        FillClientRoleDDL("");
        ShowDetailNoNav();
        $(".isnew").show();
        $("#user_email").show();
        $("#user_email").val("");
        $("#display_email").html("");
        $("#user_firstname").val("");
        $("#user_lastname").val("");
        $("#user_role").val("");
        $("#user_title").val("");
        $("#user_phone1").val("");
        $("#user_phone2").val("");
        $("#user_address1").val("");
        $("#user_address2").val("");
        $("#user_city").val("");
        $("#user_zipcode").val("");
        $("#user_state").val("");
        $("#user_country").val("");
        $(".detailadd").show();
        $(".detailsave").hide();
        $(".detaildelete").hide();
        $(".notnew").hide();
        $("#user_email").focus();
        preSave()
    };
    EditUser = function(a) {
        g = a;
        jQuery("#list").setSelection(g);
        d = !0;
        c = jQuery("#list").getRowData(a);
        a = '{seid:"' + $("#hidsearchid").val() + '", key:' + c.pkUser_ID + ", intrnl:true}";
        $.ajax({
            type: "POST", contentType: "application/json; charset=utf-8", url: "/Service/Services.aspx/AddRecentDetail", data: a, dataType: "json",
            success: function() {
            },
            error: function() {
            }
        });
        $("#hidID").val(c.pkUser_ID);
        $("#loaderwrapper").show();
        FillUserForm(c);
        $("#loaderwrapper").hide()
    };
    ResetPassword = function(a, b) {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Service/Services.aspx/ResetPassword",
            data: '{email:"' +
                a + '", origination:"' + b + '"}',
            dataType: "json",
            success: function(a) { "Forced" == b && alert(a.d) },
            error: function() {
            }
        })
    };
    SaveNewUser = function() {
        var a = !1;
        c = FillGridRowClass();
        var b = !1;
        if ((b = $("#frmuserdetails").jVal({ style: "cover", padding: 3, border: 1, wrap: !0 })) && null == c.valid)
            a = "{json:" + JSON.stringify(c) + "}", $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/Service/Services.aspx/AddUser",
                data: a,
                dataType: "json",
                success: function(a) {
                    a = jQuery.parseJSON(a.d);
                    if (0 <= a.ReturnCode) {
                        postSave();
                        $("#list").trigger("reloadGrid");
                        alert("User successfully created.");
                        for (var b = l.jqGrid("getDataIDs"), h = 0; h < b.length; h++) {
                            var d = b[h];
                            c = jQuery("#list").getRowData(d);
                            c.pkUser_ID == a.Message && (h = b.length, EditUser(d))
                        }
                        ResetPassword(c.Email, "New");
                        CancelClick()
                    } else alert(a.Message)
                },
                error: function() {
                }
            }), a = !0;
        a || alert("CHECK FIELDS HIGHLIGHTED IN RED:\n\nThere are missing or invalid values given.  please correct the identified fields, and re-submit.")
    };
    SaveUser = function() {
        var a = !1;
        c = FillGridRowClass();
        var b = !1;
        if ((b = $("#frmuserdetails").jVal({
            style: "cover",
            padding: 3,
            border: 1,
            wrap: !0
        })) && null == c.valid)
            $("#loaderwrapper").show(), a = "{json:" + JSON.stringify(c) + "}", $.ajax({
                type: "POST", contentType: "application/json; charset=utf-8", url: "/Service/Services.aspx/UpdateUser", data: a, dataType: "json",
                success: function(a) {
                    a = jQuery.parseJSON(a.d);
                    0 <= a.ReturnCode ? (jQuery("#list").setRowData(g, c), $("#list").trigger("reloadGrid"), postSave(), EditUser(g), alert("User successfully updated.")) : alert(a.Message);
                    $("#loaderwrapper").hide()
                },
                error: function() { $("#loaderwrapper").hide() }
            }),
            a = !0;
        a || (alert("CHECK FIELDS HIGHLIGHTED IN RED:\n\nThere are missing or invalid values given.  please correct the identified fields, and re-submit."), $("#loaderwrapper").hide())
    };
    DeactivateUser = function() {
        if (confirm("This action will deactivate this user and remove them from view.  Are you sure you want to proceed?")) {
            var a = !1;
            c = FillGridRowClass();
            var b = c.IsActive = !1;
            if ((b = $("#frmuserdetails").jVal({ style: "cover", padding: 3, border: 1, wrap: !0 })) && null == c.valid)
                a = "{json:" + JSON.stringify(c) + "}", $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "/Service/Services.aspx/UpdateUser",
                    data: a,
                    dataType: "json",
                    success: function(a, b) { "success" == b && (jQuery("#list").setRowData(g, c), $("#list").trigger("reloadGrid"), postSave(), CancelClick()) },
                    error: function() {
                    }
                }), a = !0;
            a || alert("CHECK FIELDS HIGHLIGHTED IN RED:\n\nThere are missing or invalid values given.  please correct the identified fields, and re-submit.")
        }
    };
    promptSave = function() {
        $(".detailadd").button("option", "disabled", !1);
        $(".detailsave").button("option",
            "disabled", !1);
        $(".detailcancel").button("option", "label", "Cancel")
    };
    preSave = function() {
        $(".detailadd").button("option", "disabled", !0);
        $(".detailsave").button("option", "disabled", !0);
        d ? $(".detailcancel").button("option", "label", "Back to User") : $(".detailcancel").button("option", "label", "Back")
    };
    postSave = function() {
        $(".detailadd").button("option", "disabled", !0);
        $(".detailsave").button("option", "disabled", !0);
        d ? $(".detailcancel").button("option", "label", "Back to User") : $(".detailcancel").button("option",
            "label", "Back")
    };
    var i = $("#advancedsearch").advSearch({
        colNames: [{ disp: "Address1", valu: "Address1", typ: "str", srchop: [] }, { disp: "Address2", valu: "Address2", typ: "str", srchop: [] }, { disp: "City", valu: "City", typ: "str", srchop: [] }, { disp: "Country", valu: "Country", typ: "str", srchop: [] }, { disp: "Created By", valu: "CreatedBy", typ: "str", srchop: [] }, { disp: "Created Date", valu: "dtCreatedDate", typ: "date", srchop: [] }, { disp: "Display Name", valu: "DisplayName", typ: "str", srchop: [] }, { disp: "Email", valu: "Email", typ: "str", srchop: [] },
            { disp: "First Name", valu: "FirstName", typ: "str", srchop: [] }, { disp: "Last Name", valu: "LastName", typ: "str", srchop: [] }, { disp: "Phone1", valu: "Phone1", typ: "str", srchop: [] }, { disp: "Phone2", valu: "Phone2", typ: "str", srchop: [] }, { disp: "PostalCode", valu: "PostalCode", typ: "str", srchop: [] }, { disp: "Role", valu: "ClientRoleName", typ: "str", srchop: [] }, { disp: "State", valu: "StateProv", typ: "str", srchop: [] }, { disp: "Title", valu: "Title", typ: "str", srchop: [] }],
        params: { andor: "&&", items: [] }
    });
    SetSearchParams = function(a) {
        if (null != a) {
            i[0].p.params =
                a;
            for (var b = 0; b < a.items.length; b++) jQuery("#advancedsearch").addRow(a.items[b]);
            0 == a.items.length && jQuery("#advancedsearch").addRow()
        } else jQuery("#advancedsearch").addRow()
    };
    FillClientRoleDDL = function(a) {
        $("#user_role").html("");
        var b = "", c = FillDropdown("UserRoles").dropDownItem;
        $.each(c, function(c, e) {
            b += "<option value='" + e + "' ";
            a == e && (b += "selected");
            b += " >" + e + "</option>"
        });
        $("#user_role").html(b)
    };
    GetSelectedItem = function(a, b) {
        d = b;
        from_outside = !0;
        var c = $("#hidsearchid").val();
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Service/Services.aspx/SelectUser",
            data: '{seid:"' + c + '", id:"' + a + '", intrnl:' + d + "}",
            dataType: "json",
            success: function(a, b) {
                $("#loaderwrapper").show();
                if ("success" == b) {
                    var c = jQuery.parseJSON(a.d);
                    null != c && (ShowDetail(), FillUserForm(c))
                }
                $("#loaderwrapper").hide()
            },
            error: function() {
                alert("An error has occured retrieving data!");
                $("#loaderwrapper").hide()
            }
        })
    };
    CreateGrid = function() {
        $("#list").jqGrid({
            datatype: function(a) {
                $("#loaderwrapper").show();
                var b =
                    $("#hidsearchid").val();
                0 < f && (a.page = f);
                paramstring = 0 < i[0].p.params.items.length ? '{seid:"' + b + '", json:' + JSON.stringify(a) + ", search:" + JSON.stringify(i[0].p.params) + "}" : '{seid:"' + b + '", json:' + JSON.stringify(a) + ", search:null}";
                $.ajax({
                    type: "POST", contentType: "application/json; charset=utf-8", url: "/Service/Services.aspx/Users", data: paramstring, dataType: "json", success: function(a, b) { "success" == b && $("#list")[0].addJSONData(jQuery.parseJSON(a.d)) },
                    error: function() {
                    }
                })
            },
            jsonReader: {
                root: "rows",
                page: "page",
                total: "total",
                records: "records",
                repeatitems: !1
            },
            colNames: columnNames,
            colModel: columnModel,
            rowNum: 15,
            rowList: [5, 10, 15, 20, 25, 30, 35, 40, 50],
            pager: jQuery("#pager"),
            gridview: !1,
            hidegrid: !1,
            onPaging: function() { resizeGrid() },
            resizeStop: function(a, b) {
                f = $("#list").getGridParam("page");
                columnModel[b - 1].width = a;
                columnModel[b - 1].widthOrg = a;
                SetVCConfig();
                $("#list").jqGrid("GridUnload");
                $("#list").jqGrid("GridDestroy");
                CreateGrid();
                $("#list").trigger("reloadGrid");
                f = 0
            },
            onSelectAll: function() {
            },
            onSelectRow: function() { $("#cb_list").removeAttr("checked") },
            gridComplete: function() {
                for (var a = jQuery("#list"), b = a.jqGrid("getDataIDs"), c = 0; c < b.length; c++) {
                    var d = b[c];
                    a.jqGrid("setRowData", d, { Edit: "<img src='/images/edit18.png' alt='chart' title='' class='gridButton' onclick=\"EditUser(" + d + ');" />' });
                    0 !== c % 2 && $("#" + d, a).addClass("alternateRow")
                }
                resizeGrid()
            },
            multiselect: !0,
            rownumbers: !1,
            sortname: "Lastname",
            sortorder: "desc",
            viewrecords: !0,
            imgpath: "/CSS/themes/neopost/images",
            caption: "",
            width: "100%",
            height: "100%",
            sortable: !0
        }).navGrid("#pager", {
            edit: !1,
            add: !1,
            del: !1,
            refresh: !1,
            view: !1,
            search: !1
        }, {}, {}, {}, { multipleSearch: !0, overlay: !1 });
        $("#list").jqGrid("filterToolbar", { autosearch: !0, searchOnEnter: !1 });
        $("#gbox_list").bind("sortstop", function(a, b) {
            "list_Edit" != b.item[0].id && (l.jqGrid("getGridParam", "remapColumns"), SetVCConfig());
            f = $("#list").getGridParam("page");
            GetVCConfig();
            $("#list").jqGrid("GridUnload");
            $("#list").jqGrid("GridDestroy");
            CreateGrid();
            $("#list").trigger("reloadGrid");
            f = 0
        })
    };
    PageFilter()
});